<?php
require( 'autoload.php' );
session_start();

$config = sqlite_open("config.db");
$gen = @sqlite_fetch_array(@sqlite_query($config, "SELECT * FROM general"));


?>
<html>

<head>

<title>Meedio WeebUI</title>

<style type="text/css">
a {color: <?php echo $gen['fncolor']?>}
td {font-size: 14}
</style>
<script language="javascript" src="js/common.js"></script>
</head>

<body bgcolor="<?php echo $gen['bgcolor']?>" style="font-family: Verdana; color: <?php echo $gen['fncolor']?>;">


<table width="600" align="center" style="border: 1px solid black;">
<tr>

<td width="100" rowspan="2">
<a href="http://www.meedio.com/">
<img src="images/logo.png" width="100" height="100" style="border-width: 0px;" />
</a>
</td>

<td valign="middle" style="font-size: xx-large; padding-left: 50px;">
<b>WeebUI</b>
</td>

</tr>
<tr>

<td align="right">
<b>Meedio Web User Interface</b>
</td>

</tr>

<?php

echo "<tr>\n";
echo "<td colspan=\"2\" style=\"border-top: thin dashed #000000;\">\n";
if (@sqlite_num_rows(@sqlite_query($config, "SELECT * FROM users")) < 1) {
  include 'setup.php';
}
elseif (!session_is_registered('user')) {
  echo "<center>\n";
  echo "<form method=\"post\" action=\"login.php\">\n";
  echo "<b>Username:</b><br>\n";
  echo "<input name=\"user\" type=\"text\"><br>\n";
  echo "<b>Password:</b><br>\n";
  echo "<input name=\"pass\" type=\"password\"><br>\n";
  echo "<input type=\"submit\" value=\"Login\">\n";
  echo "</form>\n";
  echo "</center>\n";
}

else {
  echo "<table width=100% style=\"border-bottom: thin dashed #000000;\">\n";
  echo "<tr>\n";
  echo "\n";
  echo "<td><a href=\"index.php?section=library\">Library</a></td>\n";
  if (session_is_registered('admin')) {
    echo "<td><a href=\"index.php?section=importers\">Importers</a></td>\n";
    echo "<td><a href=\"index.php?section=remote\">Remote</a></td>\n";
    echo "<td><a href=\"index.php?section=message\">Messages</a></td>\n";
    echo "<td><a href=\"index.php?section=system\">System</a></td>\n";
    echo "<td><a href=\"index.php?section=setup\">Setup</a></td>\n";
  }
  echo "<td align=right>\n";
  echo "<a href=\"logout.php\">Log out</a>\n";
  echo "</td>\n";
  echo "</tr>\n";
  echo "</table>\n";

  if (session_is_registered('admin')) {
    echo "<table width=\"100%\" style=\"border-bottom: thin dashed black;\">";
    echo "<tr>";

    echo "<td align=\"center\">";
    echo "<a href=\"do.php?what=message&msg=music.previous\">";
    echo "<img border=\"0\" src=\"images/icon-prev.png\">";
    echo "</a>";
    echo "</td>";

    echo "<td align=\"center\">";
    echo "<a href=\"do.php?what=message&msg=music.rewind\">";
    echo "<img border=\"0\" src=\"images/icon-rew.png\">";
    echo "</a>";
    echo "</td>";

    echo "<td align=\"center\">";
    echo "<a href=\"do.php?what=message&msg=music.stop\">";
    echo "<img border=\"0\" src=\"images/icon-stop.png\">";
    echo "</a>";
    echo "</td>";

    echo "<td align=\"center\">";
    echo "<a href=\"do.php?what=message&msg=music.play\">";
    echo "<img border=\"0\" src=\"images/icon-play.png\">";
    echo "</a>";
    echo "</td>";

    echo "<td align=\"center\">";
    echo "<a href=\"do.php?what=message&msg=music.pause\">";
    echo "<img border=\"0\" src=\"images/icon-pause.png\">";
    echo "</a>";
    echo "</td>";

    echo "<td align=\"center\">";
    echo "<a href=\"do.php?what=message&msg=music.fast-forward\">";
    echo "<img border=\"0\" src=\"images/icon-ff.png\">";
    echo "</a>";
    echo "</td>";

    echo "<td align=\"center\">";
    echo "<a href=\"do.php?what=message&msg=music.next\">";
    echo "<img border=\"0\" src=\"images/icon-next.png\">";
    echo "</a>";
    echo "</td>";

    echo "<tr>";
    echo "</table>";

  }

  if (!$_GET['section']) $_GET['section'] = "library";

  echo "</td>\n";
  echo "</tr>\n";
  echo "<tr>\n";
  echo "<td colspan=\"2\">\n";

  if ($_GET['section'] == "library") {
    $library = $_GET['library'];

    if (!$library) {
      echo "<br>\n";
      $sql = sqlite_query($config, "SELECT * FROM library ORDER BY id ASC");
      if (sqlite_num_rows($sql) < 1) echo "<table align=\"center\" width=\"100%\">\n<tr>\n";
      $i = 0;
      while ($l = sqlite_fetch_array($sql)) {
        $i++;
        if ($i == 1) {
          echo "<table align=\"center\" width=\"100%\">\n";
          echo "<tr>\n";
        }
        echo "<td align=\"center\">\n";
        $id = $l['id'];
        echo "<a href=\"index.php?section=library&library=$id\">\n";
        if ($l['type'] == "music") echo '<img border="0" src="images/music.gif" style="margin: 8px;" alt="'.$l['name'].'">';
        if ($l['type'] == "movies") echo '<img border="0" src="images/movies.gif" style="margin: 8px;" alt="'.$l['name'].'">';
        if ($l['type'] == "tv") echo '<img border="0" src="images/tv.gif" style="margin: 8px;" alt="'.$l['name'].'">';
        echo "<br>\n";
        echo "<b>".$l['name']."</b>\n";
        echo "</a>\n";
        echo "</td>\n";
        if ($i == 3) {
          echo "</tr>\n";
          echo "</table>\n";
          $i = 0;
        }
      }
      if (session_is_registered('admin')) {
        if ($i == 0) {
          echo "<table align=\"center\" width=\"100%\">\n";
          echo "<tr>\n";
        }
        $i++;
        echo "<td align=\"center\">\n";
        echo "<a href=\"index.php?section=themes\">\n";
        echo "<img border=\"0\" src=\"images/themes.gif\" style=\"margin: 8px;\" alt=\"Meedio Themes\">\n";
        echo "<br><b>Meedio Themes</b>";
        echo "</a>\n";
        echo "</td>\n";
      }
      if (session_is_registered('admin')) $chk = "";
      else $chk = "WHERE admin='0'";
      if (sqlite_num_rows(sqlite_query($config, "SELECT * FROM live $chk")) > 0) {
        if ($i == 0) {
          echo "<table align=\"center\" width=\"100%\">\n";
          echo "<tr>\n";
        }
        elseif ($i == 3) {
          echo "</tr>\n";
          echo "</table>\n";
          echo "<table align=\"center\" width=\"100%\">\n";
          echo "<tr>\n";
          $i = 1;
        }
        echo "<td align=\"center\">\n";
        echo "<a href=\"index.php?section=live\">\n";
        echo "<img border=\"0\" src=\"images/live.gif\" style=\"margin: 8px;\" alt=\"Live Streams\">\n";
        echo "<br><b>Live Streams</b>";
        echo "</a>\n";
        echo "</td>\n";
      }
    echo "</tr>\n";
    echo "</table>\n";
    }

    else {
      $lib = sqlite_fetch_array(sqlite_query($config, "SELECT * FROM library WHERE id='$library'"));

      if ($lib['type'] == "music") include('music.php');

      elseif ($lib['type'] == "movies") include('movies.php');

      elseif ($lib['type'] == "tv") include('tv.php');
    }
  }


  elseif ($_GET['section'] == "importers") {
    echo "<table align=\"center\" style=\"margin-top: 20px; border: 1px solid black; margin-bottom: 20px;\">\n";
    echo "<tr>\n";
    echo "<td style=\"padding-right: 20px; margin-right: 0px;\"><b>Importer Section:</b></td>\n";
    echo "<td style=\"margin-left: 0px;\"><b>Action:</b></td>\n";
    echo "</tr>\n";

    $db = sqlite_open($gen['path']."\\library\\library.imports");
    $sql = sqlite_query($db, "SELECT DISTINCT section FROM imports ORDER BY section");

    while ($data = sqlite_fetch_array($sql)) {
      echo "<tr>\n";
      echo "<td style=\"padding-right: 20px; border-top: 1px solid black; margin-right: 0px;\">".$data['section']."</td>\n";
      echo "<td style=\"border-top: 1px solid black; margin-left: 0px;\"><a href=\"do.php?what=refresh&library=".$data['section']."\">Run</a></td>\n";
      echo "</tr>\n";

    }
    echo "</table>\n";
  }


  elseif ($_GET['section'] == "remote") include 'remote.php';

  elseif ($_GET['section'] == "setup") include 'setup.php';

  elseif ($_GET['section'] == "message") {
    echo "<br><br>\n";
    echo "<form method=\"post\" action=\"do.php?what=message\">\n";
    echo "<b>Subject:</b><br>\n";
    echo "<input type=\"text\" name=\"msg\" size=\"40\"><br>\n";
    echo "<b>Parameters:</b><br>\n";
    echo "<textarea name=\"p\" cols=\"40\" rows=\"5\">\n";
    echo "</textarea>\n";
    echo "<input type=\"submit\" value=\"Send\">\n";
    echo "<br>\n";
    echo "one parameter pr line!:)\n";
    echo "</form>\n";
  }

  elseif ($_GET['section'] == "system") include 'system.php';

  elseif ($_GET['section'] == "themes") {
    include('themes.php');
  }
  elseif ($_GET['section'] == "live") {
    echo "<center><b style=\"font-size: 20;\">Live Streams</b></center><br>\n";
    if (session_is_registered('admin')) $chk = "";
    else $chk = "WHERE admin='0'";
    $sql = sqlite_query($config, "SELECT * FROM live $chk");
    while ($data = sqlite_fetch_array($sql)) {
      $id = $data['id'];
      echo "<a href=\"live.php?id=$id\" target=\"live\">";
      echo "<b style=\"margin-left: 20px; font-size: 16\">".$data['name']."</b><br>";
      echo "</a>";
    }
  }

  elseif ($_GET['section'] == "weather") {
    include 'weather.php';
  }
  elseif ($_GET['section'] == "imdbsearch") include "imdbsearch.php";
  elseif ($_GET['section'] == "imdb") include "imdb.php";
}


?>

</td>
</tr>
</table>
</body>
</html>